﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.OracleClient;
using System.Net.Mail;
using System.Net;
using System.Web.Script.Serialization;


public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest(HttpContext context)
    {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
            //抢单
            case "snatchsave":
                snatchsave(context);
                break;
            //获取抢单列表数据
            case "get_Snatch_list":
                get_Snatch_list(context);
                break;
            //获取抢单单个数据
            case "get_Snatch_list_one":
                get_Snatch_list_one(context);
                break;
            default:
                break;
        }
    }
    //抢单
    public void snatchsave(HttpContext context)
    {
        bool flag;
        string remsg = "";
        string id = PageBase.ext_string(context.Request.Params["ids"]);
        decimal? sl = PageBase.ext_decimal(context.Request.Params["sl"]);
        string mobiles = PageBase.ext_string(context.Request.Params["mobiles"]);
        string customerCodes = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string sql_snatch = "exec zdy_抢单 '{0}','{1}','{2}','{3}'";
        sql_snatch = string.Format(sql_snatch,id,sl,mobiles,customerCodes);
        DataTable dt_snatch= DBHelper.ExecuteDataTable(sql_snatch);
        if (dt_snatch.Rows[0]["remsg"].ToString() == "抢单成功")
        {
            flag = true;
            remsg = dt_snatch.Rows[0]["remsg"].ToString();
        }
        else
        {
            flag = false;
            remsg = dt_snatch.Rows[0]["remsg"].ToString();
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + remsg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();

    }
    //获取抢单列表
    public void get_Snatch_list_one(HttpContext context)
    {
        string customerCode = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string mobile= PageBase.ext_string(context.Request.Params["mobiles"]);
        string requestdocno= PageBase.ext_string(context.Request.Params["requestdocno"]);
        string tmid = PageBase.ext_string(context.Request.Params["tmid"]);

        string sqllist = string.Format("exec [zdy_查询抢单列表_one] '{0}','{1}'",requestdocno,tmid);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"id\":\"" + dtResult.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"request_storecode\":\"" + dtResult.Rows[i]["request_storecode"].ToString() + "\",");
            sb.Append("\"mobile\":\"" + dtResult.Rows[i]["mobile"].ToString() + "\",");
            sb.Append("\"takeman\":\"" + dtResult.Rows[i]["takeman"].ToString() + "\",");
            sb.Append("\"takephone\":\"" + dtResult.Rows[i]["takephone"].ToString() + "\",");
            sb.Append("\"takeaddress\":\"" + dtResult.Rows[i]["takeaddress"].ToString() + "\",");
            sb.Append("\"tmid\":\"" + dtResult.Rows[i]["tmid"].ToString() + "\",");
            sb.Append("\"value\":\"" + dtResult.Rows[i]["value"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtResult.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"ys\":\"" + dtResult.Rows[i]["ys"].ToString() + "\",");
            sb.Append("\"cmwm\":\"" + dtResult.Rows[i]["cmwm"].ToString() + "\",");
            sb.Append("\"qdsl\":\"" + dtResult.Rows[i]["qdsl"].ToString() + "\",");
                   sb.Append("\"remark\":\"" + dtResult.Rows[i]["remark"].ToString() + "\",");
            sb.Append("\"createtime\":\"" + dtResult.Rows[i]["createtime"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //获取抢单列表
    public void get_Snatch_list(HttpContext context)
    {
        string customerCode = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string mobile= PageBase.ext_string(context.Request.Params["mobiles"]);

        DataTable dtResult = DBHelper.ExecuteDataTable("exec [zdy_查询抢单列表] ");
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"id\":\"" + dtResult.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"request_storecode\":\"" + dtResult.Rows[i]["request_storecode"].ToString() + "\",");
            sb.Append("\"mobile\":\"" + dtResult.Rows[i]["mobile"].ToString() + "\",");
            sb.Append("\"takeman\":\"" + dtResult.Rows[i]["takeman"].ToString() + "\",");
            sb.Append("\"takephone\":\"" + dtResult.Rows[i]["takephone"].ToString() + "\",");
            sb.Append("\"takeaddress\":\"" + dtResult.Rows[i]["takeaddress"].ToString() + "\",");
            sb.Append("\"tmid\":\"" + dtResult.Rows[i]["tmid"].ToString() + "\",");
            sb.Append("\"value\":\"" + dtResult.Rows[i]["value"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtResult.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"ys\":\"" + dtResult.Rows[i]["ys"].ToString() + "\",");
            sb.Append("\"cmwm\":\"" + dtResult.Rows[i]["cmwm"].ToString() + "\",");
            sb.Append("\"qdsl\":\"" + dtResult.Rows[i]["qdsl"].ToString() + "\",");
            sb.Append("\"remark\":\"" + dtResult.Rows[i]["remark"].ToString() + "\",");
            sb.Append("\"createtime\":\"" + dtResult.Rows[i]["createtime"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
   
    //保存零售信息
    public void salesave(HttpContext context)
    {
        int retrun = 0;
        string remsg = "";
        string id = PageBase.ext_string(context.Request.Params["ids"]);
        decimal? sl = PageBase.ext_decimal(context.Request.Params["sl"]);
        string iscz = "select * from product where id='"+id+"' and isactive='Y'";
        DataTable  ishave= DBHelper.ExecuteDataTable(iscz);
        //生成单据编号
        string sqldjbh = "declare @BH INT;declare @MID varchar(50);SELECT @BH=ISNULL(MAX(RIGHT(djbh,4)),0)+1 FROM sale WHERE LEFT(djbh,2)='LS'  AND  RIGHT(LEFT(djbh,4),2)=YEAR(GETDATE())%100;set @MID='LS'+CAST( CONVERT(VARCHAR(10),GETDATE(),12)*10000+@BH AS VARCHAR(30));SELECT @MID";
        DataTable dtResult = DBHelper.ExecuteDataTable(sqldjbh);
        string djbh = dtResult.Rows[0][0].ToString();
        if (ishave.Rows.Count > 0)
        {
            decimal? lsprice = PageBase.ext_decimal(ishave.Rows[0]["reprice"]) == null ? 0 : PageBase.ext_decimal(ishave.Rows[0]["reprice"]);
            decimal? jhprice = PageBase.ext_decimal(ishave.Rows[0]["buyprice"]) == null ? 0 : PageBase.ext_decimal(ishave.Rows[0]["buyprice"]);
            decimal? pfprice = PageBase.ext_decimal(ishave.Rows[0]["pfprice"]) == null ? 0 : PageBase.ext_decimal(ishave.Rows[0]["pfprice"]);
            string SQLMaster = @"INSERT INTO [sale_item]
                           ([djbh]
                           ,[productid]
                           ,[sl]
                           ,[lsprice]
                           ,[jhprice]
                           ,[pfprice]
                           ,[isactive]
                            )
                     VALUES
                           ('{0}'
                           ,'{1}'
                           ,{2}
                           ,{3}
                           ,{4}
                           ,{5}
                           ,'{6}'
                           );";
            SQLMaster = string.Format(SQLMaster, djbh, id, sl,lsprice,jhprice,pfprice, "Y");

            decimal? lsje =lsprice * sl;
            decimal? jhje =jhprice * sl;
            decimal? pfje = pfprice * sl;
            SQLMaster += @"INSERT INTO [sale]
                           ([djbh]
                           ,[lssl]
                           ,[lsje]
                           ,[jhje]
                           ,[pfje]
                           ,[state]
                           ,[isactive]
                            )
                     VALUES
                           ('{0}'
                           ,{1}
                           ,{2}
                           ,{3}
                           ,{4}
                           ,'{5}'
                           ,'{6}'
                           );";

            SQLMaster = string.Format(SQLMaster, djbh,sl, lsje,jhje,pfje, 2, "Y");
            retrun = DBHelper.ExecuteNonQuery(SQLMaster);
            if (retrun > 0)
            {
                remsg += "销售成功!";
            }
            else
            {
                remsg += "销售失败!";
            }
        }
        else
        {
            remsg += "商品不存在";
        }

        context.Response.Write("{\"message\":\"" + remsg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();

    }
    
    public class GetDate
    {
        public List<main> ArrayObj_main { get; set; }//主表信息
        public List<mx> ArrayObj_mx { get; set; }//明细信息
    }
    public class main
    {
        //单据编号
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //单据编号
        public string docno;
        public string DOCNO
        {
            get { return docno; }
            set { docno = value; }
        }
        //店仓编号
        public string userid;
        public string USERID
        {
            get { return userid; }
            set { userid = value; }
        }
        //店仓编号
        public string customerCode;
        public string CUSTOMERCODE
        {
            get { return customerCode; }
            set { customerCode = value; }
        }
        //收货人姓名
        public string shr;
        public string SHR
        {
            get { return shr; }
            set { shr = value; }
        }
        //手机号
        public string phone;
        public string PHONE
        {
            get { return phone; }
            set { phone = value; }
        }
        //企微手机号
        public string mobile;
        public string MOBILE
        {
            get { return mobile; }
            set { mobile = value; }
        }
        //收货人地址
        public string address;
        public string ADDRESS
        {
            get { return address; }
            set { address = value; }
        }
        //备注
        public string remark;
        public string REMARK
        {
            get { return remark; }
            set { remark = value; }
        }

    }
    //尺码
    public class mx
    {
        //tmid
        public string tmid;
        public string TMID
        {
            get { return tmid; }
            set { tmid = value; }
        }

        //数量
        public int? sl;
        public int? SL
        {
            get { return sl; }
            set { sl = value; }
        }
        //备注
        public string mark;
        public string MARK
        {
            get { return mark; }
            set { mark = value; }
        }

        public string imageurl;
        public string IMAGEURL
        {
            get { return imageurl; }
            set { imageurl = value; }
        }

    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}